Fix ITD crash on signals with mismatched extrema counts#49
Open
deekshaNVIDIA wants to merge 1 commit into
Open
Fix ITD crash on signals with mismatched extrema counts#49deekshaNVIDIA wants to merge 1 commit into
deekshaNVIDIA wants to merge 1 commit into
Conversation
The itd_baseline_extract method crashed with a ValueError when the number of local maxima and minima differed after boundary padding, because np.vstack required equal-length arrays. Root cause: Lines 106-107 used np.append to flatten (index, value) pairs into 1D arrays, then np.vstack((LK1, LK2)) assumed both had the same length. After the boundary conditions on lines 74-88 could add different counts to idx_max vs idx_min, the arrays diverged in length causing the crash. Changes in itd.py: - Use np.column_stack to build proper (position, value) 2D arrays for LK1 and LK2, then np.vstack to merge them. This works regardless of whether the arrays have different lengths. - Fix boundary padding to use the actual signal value at the mirrored position (x[idx]) instead of reusing the first/last extremum value from the wrong envelope. - Add guard for signals with no extrema (pure trends), returning the signal as baseline with zero rotation component. - Handle division-by-zero in the piecewise slope computation when consecutive extrema have equal signal values. - Use np.concatenate with explicit int casting for index arrays to prevent float indexing errors. - Remove debug print statements. New file test_itd.py: - Add comprehensive test suite covering built-in signals, near-monotonic signals (the crash case), asymmetric extrema, pure trends, reconstruction accuracy, and the __call__ interface.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #29 — ITD's
itd_baseline_extractmethod crashed with aValueErrorwhen processing signals where the number of local maxima and minima differed after boundary padding.Root Cause
Lines 106-107 used
np.appendto flatten(index, value)pairs into 1D arrays:After the boundary condition adjustments (lines 74-88),
idx_minandidx_maxcould have different lengths (e.g., 14 vs 12 as reported), makingnp.vstackfail with:Changes
pysdkit/_itd/itd.py:np.column_stackto build proper(position, value)2D arrays for LK1 and LK2 control points, thennp.vstackto merge them — works regardless of differing array lengthsx[idx]) instead of reusing the first/last value from the wrong envelopenp.concatenatewith explicitintcasting for index arrays to prevent float indexing errorsprintstatementspysdkit/tests/test_itd.py(new):__call__interfaceTest Plan